// pages/home/home.js
import WxValidate from "../../utils/wxValidate.js";
var api = require("../../utils/api");
var cf = require("../../config.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopBack: [],
    codeNum: "",
    addrid: "",
    isShow:true//隐藏
  },
  onLoad: function(options) {
    this.setData({
      addrid: options.addrid
    })
    if (options.shopId && options.shopId.length > 0) {
      api.shopDetails(options.shopId)
        .then(res => {
          if (res.result) {
            this.setData({
              shopBack: res.body.getShopInfoDetails[0],
              isShow: res.body.getShopInfoDetails[0].status == 2 || res.body.getShopInfoDetails[0].flag==1?true:false
            })
          }
        })
    } else {
      api.shopBack({
          cityId: wx.getStorageSync("cityId")
        })
        .then(res => {
          if (res.result) {
            let len = res.body.getShopInfo.length - 1;
            let shopBack = res.body.getShopInfo[len]
            this.setData({
              shopBack,
              codeNum: wx.getStorageSync("codeNum")
            })
          }

        })
    }
  },
  handleSubmit() {
    wx.navigateTo({
      url: '/pages/writeForm/form',
    })
  },
  handleAddr() {
    let addrid = this.data.addrid
    let userInfoId = this.data.shopBack.userInfoId
    api.setDefault({
        cityId: wx.getStorageSync("cityId"),
        flag: 1,
        id: addrid,
        userInfoId: userInfoId,
        addressType:1
      })
      .then(res => {

        wx.showToast({
          title: '设置成功',
          duration: 1500,
          success: () => {
            let pages = getCurrentPages();//当前页面栈
            if (pages.length > 1) {
              let beforePage = pages[pages.length - 2];//获取上一个页面实例对象
              beforePage.addrs();//触发父页面中的方法
              wx.navigateBack()
            }
          }
        })
      })
  },
  // 图片预览
  handlePreview(e) {
    //当前图片下标
    let index = e.currentTarget.dataset.index;
    //所有图片
    let shopBack = this.data.shopBack
    if (index === 0) {
      let arr=[];
      arr.push(shopBack.shopPicUrl)
      wx.previewImage({
        urls: arr,
        current: shopBack.shopPicUrl
      })
    } else if (index === 1) {
      let arr = [];
      arr.push(shopBack.licensePicUrl)
      wx.previewImage({
        urls: arr,
        current: shopBack.licensePicUrl
      })
    } else if (index === 2) {
      let arr = [];
      arr.push(shopBack.plaquePicUrl)
      wx.previewImage({
        urls: arr,
        current: shopBack.plaquePicUrl
      })
    }
  },
})